package bavaria.hightec.gui;

import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JLabel;

import bavaria.hightec.banking.BankCustomerView;
import bavaria.hightec.banking.Currency;
import bavaria.hightec.banking.Money;
import bavaria.hightec.exceptions.OverCheckException;

public class BankCustomerLayout extends Frame {
	private BankCustomerView bank;
	private Button okButton;
	private TextField textField;
	private Button confirmButton;
	private Button cancelButton;
	private Button fundButton;
	private Button balanceButton;
	private int accountnumber;
	private TextField amount;
	private TextField currency;
	private TextField matter;
	private JLabel balanceMessage;
	private JLabel fundMessage;


	public BankCustomerLayout(BankCustomerView bank) {
		this.bank=bank;
		setLayout(new FlowLayout());
		add(textField = new TextField("Please type in your account number:", 30));
		add(okButton = new Button("OK"));
		pack();
		setLocation(200, 200);
		show();
		okButton.addActionListener(new OkButtonListener());

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
	}

	class OkButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			if(balanceMessage!=null){
				remove(balanceMessage);
				remove(fundButton);
			    remove(balanceButton);
			}
			if(fundMessage!=null)
				remove(fundMessage);
			if(bank.accountNumberExists(Integer.valueOf(textField.getText()))){
				accountnumber=Integer.valueOf(textField.getText());
				remove(textField);
				remove(okButton);
				add(fundButton = new Button("FUND"));
				fundButton.addActionListener(new FundButtonListener());
				add(balanceButton = new Button("BALANCE"));
				balanceButton.addActionListener(new BalanceButtonListener());
				pack();
			}
			else
				textField.setText("Account number does not exist");
		}

	}
	
	class FundButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0){
			remove(fundButton);
			remove(balanceButton);
			add(amount= new TextField("amount"));
			add(currency= new TextField("currency"));
			add(matter=new TextField("matter"));
			add(confirmButton=new Button("CONFIRM"));
			confirmButton.addActionListener(new ConfirmButtonListener());
			pack();
		}
	}
	
	class ConfirmButtonListener implements ActionListener{
		public void actionPerformed(ActionEvent arg0) {
			Long geld = Long.valueOf(amount.getText());
			try {
				bank.fund(accountnumber, new Money(geld,Currency.valueOf(currency.getText())), matter.getText());
			} catch (NumberFormatException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (OverCheckException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			remove(confirmButton);
			remove(currency);
			remove(matter);
			remove(amount);
			add(okButton = new Button("OK"));
			fundMessage = new JLabel("Transaction done");
	        fundMessage.setFont(new Font("Arial", Font.ITALIC, 14));
	        add(fundMessage);
	        pack();
			okButton.addActionListener(new OkButtonListener());		
		}
		
	}
	
	class BalanceButtonListener implements ActionListener {
		public void actionPerformed(ActionEvent e){
			remove(fundButton);
			remove(balanceButton);
			bank.balance(accountnumber);
			balanceMessage = new JLabel("Balance: "+ bank.balance(accountnumber));
	        balanceMessage.setFont(new Font("Arial", Font.ITALIC, 14));
	        add(balanceMessage);
	        add(okButton = new Button("OK"));
			okButton.addActionListener(new OkButtonListener());
			pack();
		}
	}

}